package com.Controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.dao.UserDao;
import com.entity.Layui;
import com.entity.User;
import com.service.UserService;
import com.util.JsonUtil;


@org.springframework.stereotype.Controller
public class Controller {
	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	
	
	@Autowired
	@Qualifier("userService")
	private UserService us;
	
	@RequestMapping("/getAll")	
	@ResponseBody
	public Layui biao(HttpServletResponse response) {  
		
		List<User> li = us.getAll();
		
		Layui<User> lay = new Layui<User>();
		lay.setCode(0);
		lay.setMsg("1");
		lay.setCount(li.size());
		lay.setData(li);
		
		JsonUtil.write(lay, response);
		return lay;	
	}
	@RequestMapping("/getUserId")
	@ResponseBody
	public User getUserId(HttpServletRequest request,HttpServletResponse response) {
		int id=Integer.parseInt(request.getParameter("id"));
		User userList=us.getUserId(id);
		JsonUtil.write(userList, response);
		return userList;
	}
	@RequestMapping("/getupdate")	
	@ResponseBody
	public String update(int id,String name,String loginName,String password,HttpServletRequest request,HttpServletResponse response){//�޸�
		
		int count=us.getupdate(id, name, loginName, password);
		  
		JsonUtil.write(count, response);
		 
		return JSON.toJSONString(count);
	}
	
	
	@RequestMapping("/getInsect")	
	@ResponseBody
	public String insert(String name, String loginName, int password,HttpServletRequest request,HttpServletResponse response){
		
		int count=us.getInsect(name, loginName, password);
		
		JsonUtil.write(count, response);
		return JSON.toJSONString(count);
		}
	@RequestMapping("/getdelete")	
	@ResponseBody
	public String delete(HttpServletRequest request,HttpServletResponse response){   //ɾ��
		
		int id= Integer.parseInt(request.getParameter("id"));
		
		int count=us.getdelete(id);
		return JSON.toJSONString(count);
	}

}
